package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Salary;
import com.ruoyi.system.mapper.EmployeeMapper;
import com.ruoyi.system.mapper.RewardsPunishmentMapper;
import com.ruoyi.system.mapper.SalaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HistorySalaryMapper;
import com.ruoyi.system.domain.HistorySalary;
import com.ruoyi.system.service.IHistorySalaryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 历史薪资Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class HistorySalaryServiceImpl implements IHistorySalaryService 
{
    @Autowired
    private HistorySalaryMapper historySalaryMapper;

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 查询历史薪资
     * 
     * @param historyId 历史薪资主键
     * @return 历史薪资
     */
    @Override
    public HistorySalary selectHistorySalaryByHistoryId(Long historyId)
    {
        return historySalaryMapper.selectHistorySalaryByHistoryId(historyId);
    }

    /**
     * 查询历史薪资列表
     * 
     * @param historySalary 历史薪资
     * @return 历史薪资
     */
    @Override
    @DataScope(deptAlias = "e", userAlias = "su")
    public List<HistorySalary> selectHistorySalaryList(HistorySalary historySalary)
    {
        return historySalaryMapper.selectHistorySalaryList(historySalary);
    }

    /**
     * 新增历史薪资
     * 
     * @param historySalary 历史薪资
     * @return 结果
     */
    @Override
    public int insertHistorySalary(HistorySalary historySalary)
    {
        historySalary.setCreateTime(DateUtils.getNowDate());
        return historySalaryMapper.insertHistorySalary(historySalary);
    }

    /**
     * 修改历史薪资
     * 
     * @param historySalary 历史薪资
     * @return 结果
     */
    @Override
    public int updateHistorySalary(HistorySalary historySalary)
    {
        historySalary.setUpdateTime(DateUtils.getNowDate());
        return historySalaryMapper.updateHistorySalary(historySalary);
    }

    /**
     * 批量删除历史薪资
     * 
     * @param historyIds 需要删除的历史薪资主键
     * @return 结果
     */
    @Override
    public int deleteHistorySalaryByHistoryIds(String historyIds)
    {
        return historySalaryMapper.deleteHistorySalaryByHistoryIds(Convert.toStrArray(historyIds));
    }

    /**
     * 删除历史薪资信息
     * 
     * @param historyId 历史薪资主键
     * @return 结果
     */
    @Override
    public int deleteHistorySalaryByHistoryId(Long historyId)
    {
        return historySalaryMapper.deleteHistorySalaryByHistoryId(historyId);
    }

    /**
     * 保存月末薪资
     */
    @Override
    @Scheduled(cron = "0 0 17 L * ?")//秒 分 时 日 月 周 年（省略不写）  (cron = "0 5 17 L * ?")
    public void saveHistory() {
        List<Long> employeeId = employeeMapper.selectAllEmployeeId();
        for(Long id : employeeId){
            Salary salary = salaryMapper.selectSalaryByEmployeeId(id);
            HistorySalary historySalary = new HistorySalary();
            historySalary.setEmployeeId(salary.getEmployeeId());
            historySalary.setEmployeeName(salary.getEmployeeName());
            historySalary.setBaseSalary(salary.getBaseSalary());
            historySalary.setInsurance(salary.getInsurance());
            historySalary.setLate(salary.getLate());
            historySalary.setBonus(salary.getBonus());
            historySalary.setFinalSalary(salary.getFinalSalary());
            historySalary.setCreateTime(new Date());
            historySalaryMapper.insertHistorySalary(historySalary);
        }
    }
}
